package com.jxwifi.cloud.quickclean.quicktreasure;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.idroid.utils.NoDoubleClickListener;
import com.idroid.utils.TextColorUtil;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.app.AccountMgr;
import com.jxwifi.cloud.quickclean.app.ClientBasicActivity;
import com.jxwifi.cloud.quickclean.app.PasswordLogin;
import com.jxwifi.cloud.quickclean.main.WebViewsActivity;
import com.jxwifi.cloud.quickclean.orderdetails.OrderDetailsActivity;
import com.jxwifi.cloud.quickclean.pay.PayResult;
import com.jxwifi.cloud.quickclean.utils.TitleClient;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;
import com.umeng.analytics.MobclickAgent;

import org.simple.eventbus.Subscriber;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BuySetMealActivity extends ClientBasicActivity implements BuySetMealView {

    @Bind(R.id.rv_buy_set_meal_list)
    RecyclerView mRvBuySetMealList;
    @Bind(R.id.tv_but_set_meal_explain)
    TextView mTvButSetMealExplain;

    private View notDataView;

    private BuySetMealPresenter mPresenter;

    private String mWXPayCode;//微信支付接口返回的code

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy_set_meal);
        ButterKnife.bind(this);
        openEventBus();
        initHead();
        mPresenter = new BuySetMealPresenter(mBaseContext, this);
        mPresenter.initView();
        mTvButSetMealExplain.setText("点确认购买，即表示同意购买说明");
        TextColorUtil.setColorSpan(mBaseContext, mTvButSetMealExplain, 11, mTvButSetMealExplain.getText().length(), R.color.color_cleaning_text);
    }

    private void initHead() {
        TitleClient holder = new TitleClient(ButterKnife.findById(this, R.id.lin_client_title));
        holder.defineTitle("套餐购买");
        holder.defineLeft(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                onBackPressed();
            }
        });
    }

    @OnClick({R.id.tv_set_meal_remark,R.id.tv_but_set_meal_explain})
    void clickCK(View v) {
        switch (v.getId()) {
            case R.id.tv_set_meal_remark:
                mPresenter.findbank();
                MobclickAgent.onEvent(mBaseContext, "tv_set_meal_remark");
                break;
            case R.id.tv_but_set_meal_explain:
                mPresenter.BuyExplain();
                MobclickAgent.onEvent(mBaseContext, "tv_but_set_meal_explain");
                break;
        }
    }

    @Subscriber(tag = "e-PayResult")
    private void getCondItion(PayResult mBean) {
        Toast toast = Toast.makeText(mBaseContext, "支付成功", Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
        Intent intent = new Intent();
        intent.setClass(mBaseContext, QuickTreasureActivity.class);
        startActivity(intent);
        finish();
    }

    @Subscriber(tag = "e-WXPayResult")
    private void getWXCondItion(PayResult mBean) {
        Toast toast = Toast.makeText(mBaseContext, "支付成功", Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
        Intent intent = new Intent();
        intent.setClass(mBaseContext, QuickTreasureActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public View getViews() {
        notDataView = getLayoutInflater().inflate(R.layout.null_title_layout, (ViewGroup) mRvBuySetMealList.getParent(), false);
        return notDataView;
    }

    @Override
    public void ononprepay(String PayType) {
        PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
        if (PasswordLogin.mIsLogin) {
            mPresenter.onprepay(PayType);
        }
    }

    @Override
    public void oninitData() {
        PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
        if (PasswordLogin.mIsLogin) {
            mPresenter.initData();
        }
    }

    @Override
    public void onBuyExplain() {
        PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
        if (PasswordLogin.mIsLogin) {
            mPresenter.BuyExplain();
        }
    }

    @Override
    public void getWXPayCode(String mOutTradNo) {
        mWXPayCode = mOutTradNo;
    }

    @Override
    public void onAuzSuccess() {
        Toast toast = Toast.makeText(mBaseContext, "充值成功", Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
        finish();
    }

    @Override
    public void onFailure(int errorCode, String msg) {
        Toast toast = Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
    }

    @Override
    public RecyclerView getRvList() {
        return mRvBuySetMealList;
    }
}
